/**
 * @author lk
 * @date 2022/6/6 9:33
 */
package com.mmall.thread.abc;

public class BankTest {

    public static void main(String[] args) throws InterruptedException {
        int N = 10;
        long start = System.currentTimeMillis();
        for (int i = 0; i < N; i++) {
            Thread thread = new Thread(BankTest::work);
            thread.start();
            thread.join();
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    private static void work() {
        Bank instance = Bank.getInstance();
        System.out.println(instance);
    }
}


class Bank {
    private static Bank instance;

    //TODO:为啥这样子写也没错？
    public static synchronized Bank getInstance() {
        if (instance==null) {
            sleep();
            synchronized (Bank.class) {
//                if (instance == null) {
                    instance = new Bank();
//                }
            }
        }
        return instance;
    }

    private static void sleep() {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
